package com.hubiao.pay.merchant.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hubiao.pay.common.util.EncryptUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/***
 * 获取当前登录用户信息
 * 前端配置token，后续每次请求并通过Header方式发送至后端
 */
public class SecurityUtil {

    //测试使用
    public static Long getMerchantId() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes()))
                .getRequest();
        String jsonToken = request.getHeader( "authorization" );
        if (StringUtils.isEmpty( jsonToken ) || !jsonToken.startsWith( "Bearer " )) {
            throw new RuntimeException( "token is not as expected" );
        }
        jsonToken = jsonToken.substring( 7 );
        jsonToken = EncryptUtil.decodeUTF8StringBase64( jsonToken );
        JSONObject jsonObject = JSON.parseObject( jsonToken );
        return jsonObject.getLong( "merchantId" );
    }


    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    public static LoginUser getUser() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();

        if (servletRequestAttributes != null) {
            HttpServletRequest request = servletRequestAttributes.getRequest();

            Object jwt = request.getAttribute( "jsonToken" );
            if (jwt instanceof LoginUser) {
                return (LoginUser) jwt;
            }
        }
        return new LoginUser();
    }

    /*	token 明文格式
        {
            "mobile": "",
                "payload": "",
                "client_id": "merchant-platform",
                "user_name": "",
                "merchantId":"1196392632578809858"
        }*/
    public static void main(String[] args) {
        String jsonToken = "Bearer ewoJIm1vYmlsZSI6ICIiLAoJInBheWxvYWQiOiAiIiwKCSJjbGllbnRfaWQiOiAibWVyY2hhbnQtcGxhdGZvcm0iLAoJInVzZXJfbmFtZSI6ICIiLAoJIm1lcmNoYW50SWQiOiIxMTk2MzkyNjMyNTc4ODA5ODU4Igp9";
        if (StringUtils.isEmpty( jsonToken ) || !jsonToken.startsWith( "Bearer " )) {
            throw new RuntimeException( "token is not as expected" );
        }

        jsonToken = jsonToken.substring( 7 );
        jsonToken = EncryptUtil.decodeUTF8StringBase64( jsonToken );
        JSONObject jsonObject = JSON.parseObject( jsonToken );
        System.out.println( jsonObject.getString( "merchantId" ) );
    }

}
